package com.freesky.biz.rights.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freesky.biz.rights.domain.RightsMobileRechargeEntity;
import com.freesky.biz.rights.domain.RightsPayEntity;
import com.freesky.biz.rights.domain.RightsReceiveEntity;
import com.freesky.biz.rights.mapper.RightsMobileRechargeMapper;
import com.freesky.biz.rights.mapper.RightsPayMapper;
import com.freesky.biz.rights.service.IRightsMobileRechargeService;
import com.freesky.biz.rights.service.IRightsPayService;
import com.freesky.biz.rights.service.IRightsReceiveService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


@Service
public class RightsMobileRechargeServiceImpl extends ServiceImpl<RightsMobileRechargeMapper, RightsMobileRechargeEntity> implements IRightsMobileRechargeService {

    @Autowired
    private RightsMobileRechargeMapper rightsMobileRechargeMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;


    @Override
    public int updateManualById(String id, String processed) {
        return rightsMobileRechargeMapper.updateManualById(id, processed);
    }

    @Override
    public Page selectRightsMobileRechargePage(Page page, RightsMobileRechargeEntity entity) {
        return rightsMobileRechargeMapper.selectRightsMobileRechargePage(page,entity);
    }

    @Override
    public List<RightsMobileRechargeEntity> selectRightsMobileRechargList(RightsMobileRechargeEntity entity) {
        return rightsMobileRechargeMapper.selectRightsMobileRechargePage(entity);
    }

    @Override
    public AjaxResult exportFile(RightsMobileRechargeEntity entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("RIGHTS_MOBILE_RECHARGE");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }
        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new RightsMobileRechargeEntity(), IRightsMobileRechargeService.class,
                "selectRightsMobileRechargList", "通话话费充值",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }
}
